package Server.Game;

import java.util.ArrayList;
import java.util.Iterator;

import Server.Sessions.ActiveSessions;
import Server.Sessions.ClientSession;
import Server.Sessions.OutboundMessages;

public class Controller extends Thread{

	ActiveSessions activeSessions;
	OutboundMessages outQueue;
	ArrayList<ClientSession> GameClients = new ArrayList<ClientSession>();
	int GameNo = 0;
	public GameStart gameStart; 
	
	public Controller(ActiveSessions activeSessions,
			OutboundMessages outQueue) {
		this.activeSessions = activeSessions;
		this.outQueue = outQueue;
		start();
}
	public void run(){
		while(true){
			synchronized(activeSessions){
			Iterator<ClientSession> itr = activeSessions.iterator();
			while(itr.hasNext()){
				ClientSession cli = itr.next();
				if(!cli.IsInGame){
					GameClients.add(cli);
					cli.IsInGame = true;
				}
				if(GameClients.size() == 2){
					gameStart = new GameStart(GameClients, outQueue, GameNo++);
					GameClients.clear();
					break;
				}
				
			}
			
			}
			
		}
	}
}